#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
        WaitforElementByName(driver,u"我的",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    try:
        WaitforElementById(driver,"com.oppo.market:id/rl_download_manager",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    time.sleep(1)
    while True:
        try:
            btns=WaitforElementsById(driver,"com.oppo.market:id/cancel",30)
        except NoSuchElementException:
            break
        else:
            btns[0].click()
            time.sleep(2)
            print "-----------------"
            target_click(driver,676.0,246.0)
            print "-----------------"
        time.sleep(1)
    driver.back()


def clickappdown(driver):
    try:
        tasks=WaitforElementsById(driver,'com.oppo.market:id/v_app_item',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in tasks:
            try:
                size=WaitforElementById(btn,"com.oppo.market:id/tv_size",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if float(size[0:-1])<20:
                continue
            try:
                down_btn=WaitforElementById(btn,'com.oppo.market:id/btn_download',30)
            except NoSuchElementException:
                continue
            else:
                down_btn.click()
                waittime=40
                while waittime:
                    try:
                        text=WaitforElementById(btn,'com.oppo.market:id/tv_down_size',5).get_attribute("text")
                    except NoSuchElementException:
                        pass
                    print text
                    if '0B/' not in text:
                        down_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"

def game(driver):
    try:
        WaitforElementByName(driver,u"游戏",30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'休闲益智',u'角色扮演',u'射击游戏',u'棋牌游戏',u'经营策略',u'动作冒险',u'体育竞速',u'音乐舞蹈', \
                   u'特色分类']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
#    driver.back()

def soft(driver):
    try:
        WaitforElementByName(driver,u"软件",30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'视频播放',u'摄影美化',u'社交通讯',u'音乐电台',u'实用工具',u'便捷生活', \
                   u'网上购物',u'金融理财',u'新闻阅读',u'教育学习',u'交通导航',u'旅游出行',  \
                   u'系统优化',u'医疗健康',u'生活娱乐']
#    gamecatetory= [u'视频播放']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
#    driver.back()
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE        # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.oppo.market'    # apk的包名
    desired_caps['appActivity'] = '.activity.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()
